import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

public class Demo11{
    public static void main(String[] args) throws InterruptedException {
        BlockingQueue<Integer> blockingQueue = new LinkedBlockingQueue<Integer>();

        Thread customer = new Thread(()->{
            while (true){
                int val = 0;
                try {
                    val = blockingQueue.take();
                    System.out.println("消费元素"+val);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        });

        Thread producer = new Thread(()->{
            for (int i = 0; i < 50; i++) {
                try {
                    int num = i;
                    System.out.println("生产一个元素" + num);
                    blockingQueue.put(num);
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        });
        customer.start();
        producer.start();
        customer.join();
        producer.join();
    }

}
